<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row class="ml10" v-show="showSearch">
				<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef">
					<el-form-item >
						<el-input placeholder="输入姓名查询" style="max-width: 180px" clearable v-model="state.queryForm.residentName" />
            <el-input placeholder="输入电话查询" style="max-width: 180px" clearable v-model="state.queryForm.phone" />
					</el-form-item>
					<el-form-item>
						<el-button @click="getDataList" icon="search" type="primary">
							查询
						</el-button>
            <el-button @click="formDialogRef.openDialog()" icon="Plus">增加</el-button>
            <el-button @click="resetQuery" icon="Refresh">
              重置
            </el-button>
            <el-button @click="handleDelete(selectObjs)" icon="Delete">
              删除
            </el-button>
          </el-form-item>
				</el-form>
			</el-row>

			<el-table
				:data="state.dataList"
				@selection-change="handleSelectionChange"
				style="width: 100%"
				v-loading="state.loading"
				border
				:cell-style="tableStyle?.cellStyle"
				:header-cell-style="tableStyle?.headerCellStyle"
			>
        <el-table-column type="selection" show-overflow-tooltip width="auto"></el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
				<el-table-column label="姓名" prop="residentName" show-overflow-tooltip />
				<el-table-column label="联系电话" prop="phone" show-overflow-tooltip />
				<el-table-column label="性别" prop="residentGender" show-overflow-tooltip >
          <template #default="scope">
            <dict-tag :options="gender" :value="scope.row.residentGender" />
          </template>
        </el-table-column>
				<el-table-column label="备注" prop="remark" show-overflow-tooltip />
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button icon="edit-pen" @click="formDialogRef.openDialog(scope.row.residentId)" text type="primary"
							>编辑
						</el-button>

						<el-button icon="delete" @click="handleDelete([scope.row.residentId])" text type="primary"
							>删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @current-change="currentChangeHandle" @size-change="sizeChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog @refresh="getDataList()" ref="formDialogRef" />
	</div>
</template>

<script lang="ts" setup>
import { BasicTableProps, useTable } from '/@/hooks/table';
import { delObj, fetchList } from '/@/api/admin/post';
import { useMessage, useMessageBox } from '/@/hooks/message';
import {getResidentList,deleteResident}  from "/@/api/resident/resident";
import {useDict} from "/@/hooks/dict";

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));

// 定义变量内容
const formDialogRef = ref();
const excelUploadRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const { gender } = useDict('gender');

const state: BasicTableProps = reactive<BasicTableProps>({
  pageList: getResidentList,
	queryForm: {
    residentName:undefined,
    phone: undefined
  },
  isPage: true,
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
  // 重置表单
  queryRef.value.resetFields();

  // 彻底清空查询条件
  state.queryForm = {
    residentName: undefined,
    phone: undefined
  };

  // 重新获取数据
  getDataList();
};

// 多选事件
const handleSelectionChange = (objs: { residentId: string }[]) => {
	selectObjs.value = objs.map(({ residentId }) => residentId);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: number[]) => {
	try {
		await useMessageBox().confirm("您确认要删除吗");
	} catch {
		return;
	}

	try {
		await deleteResident(ids);
		getDataList();
		useMessage().success("删除成功");
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>
